﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Douban.Core.Commands
{
    public class CommandBehaviorFactory
    {
        private static readonly DependencyProperty CommandBehavior =
            DependencyProperty.Register(
                "CommandBehavior",
                typeof(CommandBehaviorBase<UIElement>),
                typeof(CommandBehaviorFactory));

        public static T GetOrCreateBehavior<T>(UIElement target) where T : CommandBehaviorBase<UIElement>
        {
            var cmdBehavior = target.GetValue(CommandBehavior) as T;
            if (cmdBehavior == null)
            {
                cmdBehavior = (T)Activator.CreateInstance(typeof(T), target);
                target.SetValue(CommandBehavior, cmdBehavior);
            }

            return cmdBehavior;
        }
    }
}
